package cn.stylefeng.guns.modular.school.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.school.entity.SchoolInfo;
import cn.stylefeng.guns.modular.school.mapper.SchoolInfoMapper;
import cn.stylefeng.guns.modular.school.model.params.SchoolInfoParam;
import cn.stylefeng.guns.modular.school.model.result.SchoolInfoResult;
import cn.stylefeng.guns.modular.school.service.SchoolInfoService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 学校信息表 服务实现类
 * </p>
 *
 * @author
 * @since 2019-11-04
 */
@Service
public class SchoolInfoServiceImpl extends ServiceImpl<SchoolInfoMapper, SchoolInfo> implements SchoolInfoService {

    @Override
    public void add(SchoolInfoParam param) {
        SchoolInfo entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(SchoolInfoParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(SchoolInfoParam param) {
        SchoolInfo oldEntity = getOldEntity(param);
        SchoolInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public SchoolInfoResult findBySpec(SchoolInfoParam param) {
        return null;
    }

    @Override
    public List<SchoolInfoResult> findListBySpec(SchoolInfoParam param) {
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(SchoolInfoParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(SchoolInfoParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private SchoolInfo getOldEntity(SchoolInfoParam param) {
        return this.getById(getKey(param));
    }

    private SchoolInfo getEntity(SchoolInfoParam param) {
        SchoolInfo entity = new SchoolInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
